{ "cells": [ { "cell_type": "markdown", "id": "intro", "metadata": {}, "source": [ "# Introduction to Primitive Types and Operations in Python" ] }, { "cell_type": "markdown", "id": "concepto", "metadata": {}, "source": [ "## Step 1: What are primitive types?\n", "Primitive types are the basic elements we can work with in Python. They are the simplest data types provided by the language. Let's go through each of them one by one:\n" ] }, { "cell_type": "markdown", "id": "tipos-lista", "metadata": {}, "source": [ "- **Integers (`int`)**: Whole numbers without any decimal points. They can be positive, negative, or zero.\n", "- **Floating-point numbers (`float`)**: Numbers that have a decimal point.\n", "- **Strings (`str`)**: Text values enclosed in either single quotes (`'`) or double quotes (`\"`).\n", "- **Booleans (`bool`)**: Logical values, either `True` or `False`." ] }, { "cell_type": "markdown", "id": "enteros", "metadata": {}, "source": [ "## Step 2: Integers (`int`)\n", "Integers are numbers that do not have a decimal part. They are the most basic numeric type.\n", "### Examples:\n", "- `5` is an integer.\n", "- `-3` is a negative integer.\n", "- `0` is also an integer, which represents nothing.\n", "Integers are used when you need to represent whole numbers, such as counting items, measuring distances, or anything that doesn't require decimals.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "codigo-enteros", "metadata": {}, "outputs": [], "source": [ "# Integer operations\n", "num1 = 10\n", "num2 = 3\n", "\n", "# Addition\n", "result_addition = num1 + num2 # Adding two integers\n", "print('Addition:', result_addition) # Output: 13\n", "\n", "# Subtraction\n", "result_subtraction = num1 - num2 # Subtracting num2 from num1\n", "print('Subtraction:', result_subtraction) # Output: 7\n", "\n", "# Multiplication\n", "result_multiplication = num1 * num2 # Multiplying two integers\n", "print('Multiplication:', result_multiplication) # Output: 30\n", "\n", "# Division\n", "result_division = num1 / num2 # Dividing num1 by num2\n", "print('Division:', result_division) # Output: 3.3333" ] }, { "cell_type": "markdown", "id": "flotantes", "metadata": {}, "source": [ "## Step 3: Floating-Point Numbers (`float`)\n", "Floats represent numbers that contain a decimal point. They are used to represent real numbers that require precision beyond whole numbers.\n", "### Examples:\n", "- `3.14` is a float representing Pi.\n", "- `-2.5` is a negative float.\n", "- `0.0` is also a float representing zero with decimal precision.\n", "Floats are useful when dealing with measurements, distances, temperatures, or financial calculations that require decimals.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "codigo-flotantes", "metadata": {}, "outputs": [], "source": [ "# Float operations\n", "num3 = 3.14\n", "num4 = 2.5\n", "\n", "# Addition\n", "result_addition_float = num3 + num4 # Adding two float numbers\n", "print('Addition (float):', result_addition_float) # Output: 5.64\n", "\n", "# Subtraction\n", "result_subtraction_float = num3 - num4 # Subtracting num4 from num3\n", "print('Subtraction (float):', result_subtraction_float) # Output: 0.64\n", "\n", "# Multiplication\n", "result_multiplication_float = num3 * num4 # Multiplying two float numbers\n", "print('Multiplication (float):', result_multiplication_float) # Output: 7.85\n", "\n", "# Division\n", "result_division_float = num3 / num4 # Dividing num3 by num4\n", "print('Division (float):', result_division_float) # Output: 1.256" ] }, { "cell_type": "markdown", "id": "cadenas", "metadata": {}, "source": [ "## Step 4: Strings (`str`)\n", "Strings represent text data. Any characters enclosed in quotes, either single or double, are considered strings.\n", "### Examples:\n", "- `'Hello'` is a string.\n", "- `'Python'` is a string.\n", "- `\"Hi!\"` is also a string, using double quotes.\n", "Strings are useful when you need to represent text, such as names, addresses, messages, and more.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "codigo-cadenas", "metadata": {}, "outputs": [], "source": [ "# String operations\n", "str1 = 'Hello'\n", "str2 = 'World'\n", "\n", "# Concatenation\n", "result_concatenation = str1 + ' ' + str2 # Combine strings\n", "print('Concatenation:', result_concatenation) # Output: 'Hello World'\n", "\n", "# Repetition\n", "result_repetition = str1 * 3 # Repeat the string 3 times\n", "print('Repetition:', result_repetition) # Output: 'HelloHelloHello'" ] }, { "cell_type": "markdown", "id": "booleanos", "metadata": {}, "source": [ "## Step 5: Booleans (`bool`)\n", "Booleans represent logical values. They can only have one of two values: `True` or `False`.\n", "### Examples:\n", "- `True` represents a truth value.\n", "- `False` represents a false value.\n", "Booleans are used in logical operations and comparisons.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "codigo-booleanos", "metadata": {}, "outputs": [], "source": [ "# Boolean operations\n", "bool1 = True\n", "bool2 = False\n", "\n", "# Logical AND\n", "result_and = bool1 and bool2 # True AND False\n", "print('AND operation:', result_and) # Output: False\n", "\n", "# Logical OR\n", "result_or = bool1 or bool2 # True OR False\n", "print('OR operation:', result_or) # Output: True\n", "\n", "# Logical NOT\n", "result_not = not bool1 # NOT True\n", "print('NOT operation:', result_not) # Output: False" ] }, { "cell_type": "markdown", "id": "operaciones", "metadata": {}, "source": [ "## Step 6: Operations in Python\n", "Now, let's see how we can perform operations on these primitive types.\n", "### Operations with Integers (`int`)\n", "You can add, subtract, multiply, and divide integers just like in basic math.\n", "### Operations with Floats (`float`)\n", "Floats can be used with the same operators as integers. However, when you divide two integers, Python will return a float (even if the result is a whole number).\n", "### Operations with Strings (`str`)\n", "Strings can be concatenated (combined) using the `+` operator, or repeated using the `*` operator.\n", "### Operations with Booleans (`bool`)\n", "You can combine boolean values using logical operators like `and`, `or`, and `not`." ] }, { "cell_type": "markdown", "id": "ejercicios-operaciones", "metadata": {}, "source": [ "## Step 7: Practical Exercises on Operations\n", "Now it’s your turn! Here are some exercises to practice what you've learned about operations with primitive types.\n", "Try solving them on your own." ] }, { "cell_type": "markdown", "id": "ejercicio1", "metadata": {}, "source": [ "### Exercise 1: Simple Addition\n", "Write a program that asks the user for two numbers and adds them together.\n", "Hint: You can convert the user input into integers or floats using the `int()` or `float()` functions." ] }, { "cell_type": "markdown", "id": "ejercicio2", "metadata": {}, "source": [ "### Exercise 2: String Concatenation\n", "Write a program that asks the user for their first name and last name, then prints a message with their full name.\n", "Hint: Use string concatenation (`+`) to join the two names." ] }, { "cell_type": "markdown", "id": "ejercicio3", "metadata": {}, "source": [ "### Exercise 3: Age in 5 Years\n", "Write a program that asks the user for their age and tells them how old they will be in 5 years.\n", "Hint: Convert the age to an integer before performing the calculation." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.11.3" } }, "nbformat": 4, "nbformat_minor": 5 }